﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Comp477Project
{
    class Hud
    {
        public List<Button> buttons;
        public Texture2D button;
        public int winWidth, winHeight;
        public SpriteFont hudFont;
        public float indent, div, fps;
        public int frames;
        public double timer, prevTimer;
        public bool cloud, snow, storm, smoke, boom,ashes,waveUp,waveD, all;
        public bool[] result;

        public Hud(ref Texture2D button, int winWidth, int winHeight, SpriteFont hudFont)
        {
            cloud = snow = storm = smoke = boom =ashes=waveUp=waveD=all= false;
            result = new bool[8];
            this.button = button;
            this.winWidth = winWidth;
            this.winHeight = winHeight;
            this.buttons = new List<Button>();
            this.hudFont = hudFont;
            div = winHeight / 20;
            indent = 50;
            fps = 0;
            timer = 0;
            prevTimer = 0;
            frames = 0;
            this.buttons.Add(new Button(ref this.button, "cloud", new Vector2(indent, 2 * div), hudFont));
            this.buttons.Add(new Button(ref this.button, "snow", new Vector2(indent, 3 * div), hudFont));
            this.buttons.Add(new Button(ref this.button, "storm", new Vector2(indent, 4 * div), hudFont));
            this.buttons.Add(new Button(ref this.button, "smoke", new Vector2(indent, 5 * div), hudFont));
            this.buttons.Add(new Button(ref this.button, "boom", new Vector2(indent, 6 * div), hudFont));
            //this.buttons.Add(new Button(ref this.button, "ashes", new Vector2(indent, 7 * div), hudFont));
            this.buttons.Add(new Button(ref this.button, "waveUp", new Vector2(indent, 7 * div), hudFont));
            this.buttons.Add(new Button(ref this.button, "waveD", new Vector2(indent, 8 * div), hudFont));
            this.buttons.Add(new Button(ref this.button, "all", new Vector2(indent, 9 * div), hudFont));
        }

        public bool[] Update()
        {

            foreach (Button b in buttons)
            {
                b.Update();
            }
            for (int i = 0; i != buttons.Count(); i++)
            {
                if (buttons[i].clicked)
                {
                    // Handle Clicks
                    if (i == 0)
                    {
                        cloud = !cloud;
                    }
                    if (i == 1)
                    {
                        snow = !snow;
                    }
                    if (i == 2)
                    {
                        storm = !storm;
                    }
                    if (i == 3)
                    {
                        smoke = !smoke;
                    }
                    if (i == 4)
                    {
                        boom = !boom;
                    }
                    
                    if (i == 5)
                    {
                        waveUp = !waveUp;
                    }
                    if (i == 6)
                    {
                        waveD = !waveD;
                    }
                    if (i == 7)
                    {
                        all = !all;
                    }
                    buttons[i].Reset();
                }
            }
            //bool[] result = { cloud, snow, storm, smoke, boom,waveUp,waveD,all };
            result[0] = cloud;
            result[1] = snow;
            result[2] = storm;
            result[3] = smoke;
            result[4] = boom;
            result[5] = waveUp;
            result[6] = waveD;
            result[7] = all;
            return result;
        }

        public void Draw(SpriteBatch spriteBatch, ref Camera cam, GameTime gameTime)
        {
            //String selection = "";
            //switch (MainGame.STATE)
            //{
            //    case MainGame.GAME_STATE.TITLE:
            //        selection = "";
            //        break;
            //}
            spriteBatch.Begin();
            // Menu
            Color color = Color.White;
            //DrawString(spriteBatch, hudFont, selection, new Vector2(winWidth / 2 - 125, 0 * div), color);
            //DrawString(spriteBatch, hudFont, "Select Behaviour:", new Vector2(10, 0.8f * div), color);
            //DrawString(spriteBatch, hudFont, "Add/Remove:", new Vector2(10, 5.8f * div), color);
            //spriteBatch.DrawString(hudFont, "R2:", new Vector2(40, 2.6f * div), Color.Black);
            //DrawString(spriteBatch, hudFont, "Reposition:", new Vector2(10, 8.8f * div), color);
            foreach (Button b in buttons)
            {
                b.Draw(spriteBatch);
            }
            // Control Tips
            //DrawString(spriteBatch, hudFont, "Left Click + Mouse: Move Camera", new Vector2(5, winHeight - 2 * div), color);
            //DrawString(spriteBatch, hudFont, "Mousewheel Scroll: Zoom In-Out", new Vector2(5, winHeight - 1 * div), color);
            // FPS
            timer += gameTime.TotalGameTime.TotalSeconds - prevTimer;
            prevTimer = gameTime.TotalGameTime.TotalSeconds;
            frames++;
            if (timer > 1)
            {
                fps = frames;
                timer = 0;
                frames = 0;
            }
            DrawString(spriteBatch, hudFont, "FPS: " + (float)(int)(fps * 10) / 10, new Vector2(winWidth - 100, 0 * div), color);
            // Debug Info
            //DrawString(spriteBatch, hudFont, "Camera Zoom: " + (float)(int)(cam.distance * 100) / 100, new Vector2(winWidth - 200, winHeight - 1 * div), color);
            //DrawString(spriteBatch, hudFont, "Characters: " + chars, new Vector2(winWidth - 200, winHeight - 1 * div), color);
            spriteBatch.End();
        }

        private void DrawString(SpriteBatch s, SpriteFont f, string str, Vector2 pos, Color color)
        {
            s.DrawString(f, str, pos, Color.Black);
            s.DrawString(f, str, pos + new Vector2(2, 2), color);
        }
    }
}
